
package com.idealighter.game.games.foursshop.struct;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import com.idealighter.game.games.common.AbstractSeat;
import com.idealighter.game.games.foursshop.dto.BetsInfo;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 4s店座位 .
 */
public class FoursShopSeat extends AbstractSeat {
  // 所属桌子
  public final int order;
  public final FoursShopTable table;
  // 是否游戏中,点击开始后就是在游戏中
  public boolean gaming = false;
  // 4s下注的筹码数 由bets计算而来
  public long bet = 0;
  // 当前4s下注的筹码
  public Map<Integer, BetsInfo> bets = Maps.newConcurrentMap();
  // 上一局4s的下注筹码
  public Map<Integer, BetsInfo> prebets = Maps.newConcurrentMap();
  // 当前结算筹码(压中后获得的)
  public long billChips = 0;
  // 是否是店主
  public boolean isbanker = false;
  // 玩家获得的彩金
  public long mosaci = 0;
  // 是否自动续注
  public boolean isContinueBet = false;

  /**
   * 位置.
   * 
   * @param order .
   * @param table .
   */
  public FoursShopSeat(int order, FoursShopTable table) {
    super(order);
    this.order = order;
    this.table = table;
  }

  /**
   * 重置座位数据 .
   */
  public void reset() {
    this.gaming = false;
    this.bet = 0;
    this.billChips = 0;
    this.totalChips = 0;
    this.mosaci = 0;
    this.state = null;
    this.isbanker = false;
  }

  /**
   * 清空座位数据 .
   */
  public void clear() {
    this.playerId = 0;
    this.playerName = null;
    reset();
  }


  public Map<Integer, BetsInfo> getBets() {
    return bets;
  }

  public void setBets(Map<Integer, BetsInfo> bets) {
    this.bets = bets;
  }

  /**
   * .
   * 
   * @return
   */
  public List<BetsInfo> getBetsInfo() {
    List<BetsInfo> info = Lists.newArrayList();
    info.addAll(this.bets.values().stream().filter(betsInfo -> betsInfo != null)
        .collect(Collectors.toList()));
    return info;
  }

  /**
   * 获取上一局的下注信息 .
   *
   * @return
   */

  public Map<Integer, BetsInfo> getPrebets() {
    this.prebets.clear();
    for (Map.Entry<Integer, BetsInfo> entry : bets.entrySet()) {
      int key = entry.getKey();
      BetsInfo info = entry.getValue();
      this.prebets.put(key, info);
    }
    return this.prebets;
  }


  /**
   * 通过下注图标查找该图标下注的筹码 .
   *
   * @param carId .
   * @return
   */
  public long getBetByCarId(int carId) {
    for (BetsInfo betsInfo : this.bets.values()) {
      if (betsInfo != null) {
        if (betsInfo.getCarId() == carId) {
          return betsInfo.getBet();
        }
      }
    }
    return 0;
  }

  @SuppressWarnings("unchecked")
  @Override
  public FoursShopTable table() {
    return table;
  }
}
